package cn.com.roy.thread.t3_1_11.stack_2_new_finaly;

import java.util.ArrayList;
import java.util.List;

public class MyStack {
    private List list = new ArrayList();

    public synchronized void push() {
        while (list.size() == 1) {
            try {
                System.out.println("push 线程进入等待 ");
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        list.add("anyString = " + Math.random());
        System.out.println("push = " + list.size());
        this.notifyAll();
    }

    public synchronized String pop() {
        while (list.size() == 0) {
            try {
                System.out.println("pop 线程进入等待 ");
                this.wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        String returnValue = list.get(0).toString();
        list.remove(0);
        System.out.println("pop = " + list.size());
        this.notifyAll();
        return returnValue;
    }
}
